#-*- encoding: utf-8 -*-

from django.db import models
from django.utils.translation import ugettext_lazy as _
import datetime
from ckeditor.fields import RichTextField
from django.contrib.auth.models import User

class Reporter(models.Model):
    full_name = models.CharField(_('full_name'), max_length=40)

    def __unicode__(self):
        return self.full_name
    
    class Meta:
        verbose_name = _('reporter')
        verbose_name_plural = _('reporter')
    

class Category(models.Model):
    category = models.CharField(_('category'), max_length=40)
    
    def __unicode__(self):
        return self.category
    
    class Meta:
        verbose_name = _('category')
        verbose_name_plural = _('category')
    

class Article(models.Model):
    pub_date = models.DateTimeField(_('published_date'))
    category = models.ForeignKey(Category, verbose_name=_('category'))
    header = models.CharField(_('header'), max_length=40)
    content = RichTextField(verbose_name =_('content'), config_name='default')
    photo = models.FileField(_('photo'), upload_to='article_images/')
    reporter = models.ForeignKey(Reporter, verbose_name=_('reporter'))
    read_count = models.IntegerField(_('read_count'), default=0, editable=False, blank=True)
    comment_count = models.IntegerField(_('comment_count'), default=0, editable=False, blank=True)

    def __unicode__(self):
        return self.header
    
    class Meta:
        verbose_name = _('article')
        verbose_name_plural = _('article')
    
class Vocabulary(models.Model):
    english_word = models.CharField(_('english_word'), max_length=30)
    mongolian_word = models.CharField(_('mongolian_word'), max_length=30)
    
    def __unicode__(self):
        return unicode(self.english_word)
    
    class Meta:
        verbose_name = _('vocabulary')
        verbose_name_plural = _('vocabulary')
    
    
class Poll(models.Model):
    question = models.CharField(_('question'), max_length=200)
    pub_date = models.DateField(_('pub_date'))
    
    def __unicode__(self):
        return unicode(self.question)
    
    def was_published_today(self):
        return self.pub_date.date() == datetime.date.today()
    was_published_today.short_description = _('published_today')
    
    class Meta:
        verbose_name = _('poll')
        verbose_name_plural = _('poll')
    

class Choice(models.Model):
    poll = models.ForeignKey(Poll, verbose_name=_('poll'))
    choice = models.CharField(_('choice'), max_length=200)
    votes = models.IntegerField(_('votes'), default=0, editable=False, blank=True)
    
    def __unicode__(self):
        return unicode(self.choice)
    
    class Meta:
        verbose_name = _('choice')
        verbose_name_plural = _('choice')
    
    
class File(models.Model):
    file_name = models.CharField(_('file_name'), max_length=50)
    file_description = RichTextField(verbose_name =_('file_description'), config_name='default')
    file_image = models.FileField(_('file_image'), upload_to='file_images')
    file = models.FileField(_('file'), upload_to='files')
    upload_date = models.DateField(_('upload_date'))
    
    def __unicode__(self):
        return unicode(self.file_name)
    
    class Meta:
        verbose_name = _('file')
        verbose_name_plural = _('file')
    

class University(models.Model):
    CATEGORY_CHOICES = (
        ('1',u'АНУ, Канадын'),
        ('2',u'Их Британи, Австрали'),
        ('3',u'Европ'),
        ('4',u'Ази')
    )
    
    uni_name = models.CharField(_('uni_name'), max_length=50)
    uni_category = models.CharField(_('uni_category'), max_length=2, choices=CATEGORY_CHOICES)
    uni_article_category = models.ForeignKey(Category, verbose_name=_('category'))
    uni_address = RichTextField(verbose_name =_('uni_address'), config_name='default')
    uni_about = RichTextField(verbose_name =_('uni_about'), config_name='default')
    uni_schedule = RichTextField(verbose_name =_('uni_schedule'), config_name='default')
    uni_scholarship = RichTextField(verbose_name =_('uni_scholarship'), config_name='default')
    uni_image = models.FileField(_('uni_image'), upload_to='university_images')
    
    def __unicode__(self):
        return unicode(self.uni_name)
    
    class Meta:
        verbose_name = _('university')
        verbose_name_plural = _('university')
    

class Institution(models.Model):
    ins_name = models.CharField(_('ins_name'), max_length=50)
    ins_category = models.ForeignKey(Category, verbose_name=_('category'))
    ins_address = RichTextField( verbose_name =_('ins_address'), config_name='default')
    ins_web = models.CharField(_('ins_web'), max_length=50)
    ins_email = models.EmailField(_('ins_email'), max_length=50)
    ins_phone = models.IntegerField(_('ins_phone'))
    ins_intro = RichTextField(verbose_name =_('ins_intro'), config_name='default')
    ins_schedule = RichTextField(verbose_name =_('ins_schedule'), config_name='default')
    ins_image= models.FileField(_('ins_image'), upload_to='institution_images')
    
    def __unicode__(self):
        return unicode(self.ins_name)
    
    class Meta:
        verbose_name = _('institution')
        verbose_name_plural = _('institution')
    
    
class About(models.Model):
    about = RichTextField(verbose_name =_('about'), config_name='default')
    
    def __unicode__(self):
        return unicode(self.about)
    
    class Meta:
        verbose_name = _('about')
        verbose_name_plural = _('about')
    

class Contact(models.Model):
    contact = RichTextField(verbose_name =_('contact'), config_name='default')
    
    def __unicode__(self):
        return unicode(self.contact)
    
    class Meta:
        verbose_name = _('contact')
        verbose_name_plural = _('contact')
    
    
class Comment(models.Model):
    created = models.DateTimeField(_('created'), auto_now_add=True)
    author = models.CharField(_('author'), max_length=60)
    body = models.TextField(_('body'))
    article = models.ForeignKey(Article, verbose_name=_('article'), blank=True, null=True)
    file = models.ForeignKey(File, verbose_name=_('file'), blank=True, null=True)
    university = models.ForeignKey(University, verbose_name=_('university'), blank=True, null=True)
    institution = models.ForeignKey(Institution, verbose_name=_('institution'), blank=True, null=True)
    
    def __unicode__(self):
        return unicode("%s: %s" % (self.author, self.body[:60]))
    
    class Meta:
        verbose_name = _('comment')
        verbose_name_plural = _('comment')
    
    
class Saved_article(models.Model):
    article_id = models.ForeignKey(Article, verbose_name=_('article'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.article_id.id)
    

class Saved_university(models.Model):
    university_id = models.ForeignKey(University, verbose_name=_('university'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.university_id.id)
    
    
class Saved_institution(models.Model):
    institution_id = models.ForeignKey(Institution, verbose_name=_('institution'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.institution_id.id)
    

class Read_article(models.Model):
    article_id = models.ForeignKey(Article, verbose_name=_('article'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.article_id.id)
    

class Read_university(models.Model):
    university_id = models.ForeignKey(University, verbose_name=_('university'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.university_id.id)
    
    
class Read_institution(models.Model):
    institution_id = models.ForeignKey(Institution, verbose_name=_('institution'))
    user_id = models.ForeignKey(User, verbose_name=_('user'))
    
    def __unicode__(self):
        return unicode("%d" % self.institution_id.id)
    
    
class Banner(models.Model):
    BANNER_CHOICES = (
      ('t',_('top')),
      ('b',_('bottom')),
      ('rl',_('right_long')),
      ('rs',_('right_short'))
    )
    
    banner = models.FileField(_('banner'), upload_to='banners/')
    banner_category = models.CharField(_('banner_category'), max_length=2, choices=BANNER_CHOICES)

    def __unicode__(self):
        return unicode("%s" % self.banner)
    
    class Meta:
        verbose_name = _('banner')
        verbose_name_plural = _('banner')
        
        
class About_toefl(models.Model):
    about = RichTextField(verbose_name =_('about_toefl'), config_name='default')
    
    def __unicode__(self):
        return unicode(self.about)
    
    class Meta:
        verbose_name = _('about_toefl')
        verbose_name_plural = _('about_toefl')